﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;

namespace UserInterfaceLayer.User
{
    public partial class MonthView : System.Web.UI.Page
    {
        DateTime Current;

        protected void Page_Load(object sender, EventArgs e)
        {
            PersianCalendar pCalendar = new PersianCalendar();
            string strCurrent = Request.QueryString["d"];
            if (!PersianDateTimeExtension.TryParseHijriSolar(strCurrent, out Current))
                Response.Redirect("MonthView.aspx?d=" + DateTime.Now.ToShortHijriSolarDateString());

            int CurrentPersianDay = pCalendar.GetDayOfMonth(Current);
            int CurrentPersianMonth = pCalendar.GetMonth(Current);
            int CurrentPersianYear = pCalendar.GetYear(Current);

            if (CurrentPersianDay != 1)
                Response.Redirect("MonthView.aspx?d=" + Current.AddDays(1 - CurrentPersianDay).ToShortHijriSolarDateString());

            lblCurrent.Text = PersianDateTimeExtension.PersianMonthNames()[CurrentPersianMonth - 1] + " " + CurrentPersianYear;

            DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
            List<BusinessLogicLayer.DataObject.Day> Days = new List<BusinessLogicLayer.DataObject.Day>();
            for (int i = 0; i < pCalendar.GetDaysInMonth(CurrentPersianYear, CurrentPersianMonth); i++)
            {
                DateTime current = Current.AddDays(i);
                int currentPersianDay = pCalendar.GetDayOfMonth(current);
                int currentPersianMonth = pCalendar.GetMonth(current);
                int currentPersianYear = pCalendar.GetYear(current);
                int currentPersianDayOfWeek = ((((int)current.DayOfWeek) + 1) % 7) + 1;
                Days.Add(new BusinessLogicLayer.DataObject.Day(currentPersianDay.ToString(), PersianDateTimeExtension.PersianDayOfWeekNames()[currentPersianDayOfWeek - 1],
                        current, BusinessLogicLayer.DataObject.ReserveRoom.Convert(DataAccessLayer.Room.ToList(db), current, current.AddDays(1).AddMilliseconds(-1), db)));
            }
            lvDays.DataSource = Days;
            lvDays.DataBind();
        }

        protected void btnPrev_Click(object sender, EventArgs e)
        {
            Response.Redirect("MonthView.aspx?d=" + Current.AddDays(-31).ToShortHijriSolarDateString());
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            Response.Redirect("MonthView.aspx?d=" + Current.AddDays(31).ToShortHijriSolarDateString());
        }
    }
}